// pages/address/address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressflag:false,
    addlist:[],
    name1:"",
    dianh:"",
    shouh:"",
    editsflag:false,
    editindex:"",
    status:0,
    statusty:0
  },
  xingm:function(e){
    console.log(e.detail.value);
    this.setData({
      name1:e.detail.value
    })
  },
  dianh:function(e){
    console.log(e.detail.value);
    this.setData({
      dianh:e.detail.value
    })
  },
  shouh:function(e){
    console.log(e.detail.value);
    this.setData({
      shouh:e.detail.value
    })
  },
  adds(){
    this.setData({
      addressflag:true,
    })
    
  },
  //完成
  finishs(){
    let addlist = wx.getStorageSync('addlist');
    let addata = {
      name:this.data.name1,
      phome:this.data.dianh,
      adr:this.data.shouh,
      status:0
    }
      addlist.push(addata);
      wx.setStorageSync('addlist', addlist)
    this.setData({
      addressflag:false,
      addlist:addlist,
      name1:"",
      dianh:"",
      shouh:""

    })
   
  },
  //取消
  cancels(){
    this.setData({
      addressflag:false,
      name1:"",
      dianh:"",
      shouh:"",
      editsflag:false,
    })
  },

  edit(e){
    let addlist = wx.getStorageSync('addlist')
    let index = e.target.dataset.index
    // console.log(index);
    console.log(addlist);
    this.setData({
      addressflag:true,
      editsflag:true,
      name1:addlist[index].name,
      dianh:addlist[index].phome,
      shouh:addlist[index].adr,
      editindex:index
    })
  },
    // 完成修改
    edits(){
    let addlist = wx.getStorageSync('addlist')
    let obj = {
      name:this.data.name1,
      phome:this.data.dianh,
      adr:this.data.shouh,
      status:0
    }
  
    addlist.splice(this.data.editindex,1,obj)
    console.log(addlist);
    wx.setStorageSync('addlist', addlist)
    this.setData({
      addressflag:false,
      editsflag:false,
      addlist:addlist,
      name1:"",
      dianh:"",
      shouh:"",
    })
    },
    // 删除
  dele(e){
    console.log(e.target.dataset.index);
    let index = e.target.dataset.index
    let addlist = wx.getStorageSync('addlist');
    addlist.splice(index,1)
    wx.setStorageSync('addlist', addlist)
    this.setData({
      addlist:addlist
    })
  },
  //使用地址
  uses(e){
    console.log(222222);
    console.log(e);
    let index = e.currentTarget.dataset.index;
    let addlist = wx.getStorageSync('addlist');
    let obj = {
      name:addlist[index].name,
      phome:addlist[index].phome,
      adr:addlist[index].adr,
      status:1
    }
    addlist.forEach((item)=>{
      item.status = 0
      this.setData({
        statusty:index
      })

    })
    // console.log(addlist);
    addlist.splice(index,1,obj)
    wx.setStorageSync('addlist', addlist)
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let addlist = wx.getStorageSync('addlist');
    if(addlist){
      this.setData({
        addlist:addlist
      })
    }else{
      let addlist = []
    wx.setStorageSync('addlist', addlist)
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let addlist = wx.getStorageSync('addlist');
    // console.log(addlist);
    addlist.forEach((item,i)=>{
      if(item.status == 1){
        console.log(i);
       this.setData({
        statusty:i
       })
      }else if(item.status == 0){
        this.setData({
          statusty:"i"
         })
      }
    })
    this.setData({
      addlist:addlist
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})